//
//  UIColor+Theme.swift
//  ACFrameworkSwift
//
//  Created by c5342925 on 2022/3/31.
//

import Foundation

extension UIColor {
    
    /// Provide dynamic color from various theme.
    /// - Parameter colorStyle:enum of `ACColorStyle`.
    /// - Returns: instance of UIColor.
    public static func preferredDynamicColor(colorStyle: ACColorStyle) -> UIColor? {
        return UIColor { theme in theme.type.hexColor(style: colorStyle) }
    }
    
}

private var UIColorThemeProviderAssociatedKey: Void?

extension UIColor {
    
    /// A closure for return dynamic color according to themes.
    var themeProvider: ACThemeDynamicProviderClosure<UIColor>? {
        get {
            return objc_getAssociatedObject(self, &UIColorThemeProviderAssociatedKey) as? ACThemeDynamicProviderClosure<UIColor>
        }
        set {
            objc_setAssociatedObject(self, &UIColorThemeProviderAssociatedKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }
    
    /**
     Initialization function.
     
     like as: `init(dynamicProvider: @escaping (UITraitCollection) -> UIColor)`
     */
    public convenience init(themeProvider: @escaping ACThemeDynamicProviderClosure<UIColor>) {
        self.init()
        self.themeProvider = themeProvider
    }
    
}
